package bob.project.object.enemyes;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import bob.project.ResourceManager;
import bob.project.object.Bullet;
import bob.project.object.EnemyObject;
import bob.project.object.LiveObject;
import bob.project.slick.GameState;

public class Smit extends EnemyObject {

	Animation move_left;
	Animation move_right;
	
	Image stay_left;
	Image stay_right;
	
	Image dead_left;
	Image dead_right;
	
	public Smit()
	{
		setSpeed(40f);
		move_left  = new Animation();
		for(int i = 1;i< 5;i++)
			move_left.addFrame(ResourceManager.getImage("smitt_wolking_f0"+i).getFlippedCopy(true, false), 200);
		
		move_right  = new Animation();
		for(int i = 1;i< 5;i++)
			move_right.addFrame(ResourceManager.getImage("smitt_wolking_f0"+i), 200);
		
		
		stay_left = ResourceManager.getImage("smitt_wolking_f01").getFlippedCopy(true, false);
		stay_right = ResourceManager.getImage("smitt_wolking_f01");
		
	
		dead_left = ResourceManager.getImage("smitt_wolking_f01");
		dead_right = dead_left.getFlippedCopy(true, false);
	}
	
	@Override
	public void die()
	{
		if (isDead)
			return;
		isDead = true;
		timeOfDeath = System.currentTimeMillis();
		action = EnemyObject.ACTION_IDLE;
	}

	@Override
	public void update(int delta)
	{
		if (isDead)
		{
			if (System.currentTimeMillis() - timeOfDeath >= 3000L)
				removed = true;
			return;
		}
		gravityForce(delta);
		
		float diffX = GameState.instance.player.x - x;
		float diffY = GameState.instance.player.y - y;
		if (diffX < 0)
			moveLeft = true;
		else
			moveLeft = false;
		
		if (diffX < 0)
			diffX = -diffX;
		
		if (diffX <=256)
			action = EnemyObject.ACTION_ATTACK;
		else
			action = EnemyObject.ACTION_MOVE;
		
		if (action == EnemyObject.ACTION_MOVE)
		{
			if (moveLeft)
			{
				if (tryMoveTo(x-getSpeed()/1000f*delta,y))
				{
					isMove = true;
					x-=getSpeed()/1000f*delta;
				}
				else
				{
					if (diffY < -16f && !(tryMove(x-getSpeed()/1000f*delta,y) instanceof EnemyObject))
						jump();
				}
				move_left.update(delta);
			}
			else
			{
				if (tryMoveTo(x+getSpeed()/1000f*delta,y))
				{
					isMove = true;
					x+=getSpeed()/1000f*delta;
				}
				else
				{
					if (diffY < -16f && !(tryMove(x+getSpeed()/1000f*delta,y) instanceof EnemyObject))
						jump();
				}
				move_right.update(delta);
			}
		}
		else if (action == EnemyObject.ACTION_ATTACK)
		{
			attack(GameState.instance.player);
		}
		shape.setX(x);
		shape.setY(y);
	}

	@Override
	public void draw(Graphics g)
	{
		if (moveLeft)
		{
			if (isDead)
			{
				dead_left.draw(x+width/2 - dead_left.getWidth()/2, y+height/2-dead_left.getHeight()/2);
			}
			else if (action == EnemyObject.ACTION_MOVE)
				move_left.draw(x+width/2 - move_left.getCurrentFrame().getWidth()/2, y+height/2-move_left.getCurrentFrame().getHeight()/2);
			else
				stay_left.draw(x+width/2 - stay_left.getWidth()/2, y+height/2-stay_left.getHeight()/2);
		}
		else
		{
			if (isDead)
			{
				dead_right.draw(x+width/2 - dead_right.getWidth()/2, y+height/2-dead_right.getHeight()/2);
			}
			else if (action == EnemyObject.ACTION_MOVE)
				move_right.draw(x+width/2 - move_right.getCurrentFrame().getWidth()/2, y+height/2-move_right.getCurrentFrame().getHeight()/2);
			else
				stay_right.draw(x+width/2 - stay_right.getWidth()/2, y+height/2-stay_right.getHeight()/2);
		}
	}

	@Override
	public void attack(LiveObject target)
	{
		if (reload < System.currentTimeMillis())
		{
			//target.hit(2+GameState.rnd.nextInt(5), this);
			Bullet bul = new Bullet();
			if (!moveLeft)
				bul.setDimensions(x+24, y+24, 4, 2);
			else
				bul.setDimensions(x-6, y+24, 4, 2);	
			bul.owner = this;
			bul.damage = 50;
			bul.setSpeed(256+ GameState.rnd.nextInt(64));
			if (moveLeft)
				bul.angle = 270f + (-3f + GameState.rnd.nextFloat()*6);
			else
				bul.angle = 90f + (-3f + GameState.rnd.nextFloat()*6);
			GameState.instance.objects.add(bul);
			reload = System.currentTimeMillis() + reloadTime;
		}
	}

}
